'''
将requests中的get、post封装一下
1、使用Session发送请求，自动管理cookie
2、增加异常处理，发送请求失败时，不会中断执行，且能打印异常
'''

import requests

class BaseRequests:
    def __init__(self):

        self.session = requests.Session()

    def get(self,url,**kwargs):
        try:
            r = self.session.get(url,**kwargs)
            print(f"发送get请求成功，url为：{url},参数为：{kwargs}")
            print(f"发送get请求成功，响应为：{r.text}")
            return r
        except Exception as e:
            print(f"发送get请求异常，url为：{url},参数为：{kwargs},异常信息为:{e}")

    def post(self, url, **kwargs):
        try:
            r = self.session.post(url, **kwargs)
            print(f"发送post请求成功，url为：{url},参数为：{kwargs}")
            print(f"发送post请求成功，响应为：{r.text}")
            return r
        except Exception as e:
            print(f"发送post请求异常，url为：{url},参数为：{kwargs},异常信息为:{e}")



if __name__ =='__main__':
    b = BaseRequests()
    url = "http://192.168.150.96:8088/futureloan/mvc/api/member/register"
    cs = {"mobilephone":"15423454321","pwd":"12345"}
    b.get(url,params = cs)
    b.post(url,data = cs)
